package com.ph.irp.base.assistantmaterial.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialBalancedeadlineDAO;
import com.ph.irp.model.PBaseAssistantmaterialBalancedeadline;
import com.ph.util.DBUtil;

public class PBaseAssistantmaterialBalancedeadlineDAOImpl implements
		PBaseAssistantmaterialBalancedeadlineDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8896733280858199032L;
	// 数据源
	private ComboPooledDataSource dataSource;

	// setter for dataSource
	public void setDataSource(ComboPooledDataSource dataSource) {

		this.dataSource = dataSource;

	}

	/**
	 * 新增结算期限
	 */
	@Override
	public void addBalancedeadline(
			PBaseAssistantmaterialBalancedeadline balancedeadlineModel)
			throws Throwable {
		// TODO Auto-generated method stub
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 新增sql语句
		StringBuilder sql = new StringBuilder();

		sql
				.append("insert into p_base_assistantmaterial_balancedeadline(BalanceDeadLineCode,BalanceDeadLineName) ");
		sql.append("values('");
		sql.append(balancedeadlineModel.getBalanceDeadLineCode() + "','");
		sql.append(balancedeadlineModel.getBalanceDeadLineName() + "')");
		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql.toString());

			statement.executeUpdate();

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
	}

	/**
	 * 批量删除结算期限
	 */
	@Override
	public void deleteBalancedeadline(String balancedeadlineId) throws Throwable {
		// TODO Auto-generated method stub
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "delete from p_base_assistantmaterial_balancedeadline where BalanceDeadLineID=?";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);

			statement.setInt(1, Integer.parseInt(balancedeadlineId));

			statement.execute();

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}

	}

	/**
	 * 查询所有的结算期限
	 */
	@Override
	public List<PBaseAssistantmaterialBalancedeadline> getAllBalancedeadlines()
			throws Throwable {
		// TODO Auto-generated method stub
		// 领料用途列表
		List<PBaseAssistantmaterialBalancedeadline> deadlineList = new ArrayList<PBaseAssistantmaterialBalancedeadline>();
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "select * from p_base_assistantmaterial_balancedeadline ";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);

			resultSet = statement.executeQuery();
			// 处理结果集中的记录到采购价格model
			while (resultSet.next()) {
				PBaseAssistantmaterialBalancedeadline deadlineModel = new PBaseAssistantmaterialBalancedeadline();

				deadlineModel.setBalanceDeadLineId(resultSet.getInt(1));

				deadlineModel.setBalanceDeadLineCode(resultSet.getString(2));

				deadlineModel.setBalanceDeadLineName(resultSet.getString(3));

				deadlineList.add(deadlineModel);

			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return deadlineList;
	}

	/**
	 * 根据code模糊查询结算期限
	 */
	@Override
	public List<PBaseAssistantmaterialBalancedeadline> getBalancedeadlineByCode(
			String balancedeadlineCode) throws Throwable {
		// TODO Auto-generated method stub
		List<PBaseAssistantmaterialBalancedeadline> deadlineList = new ArrayList<PBaseAssistantmaterialBalancedeadline>();
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "select * from p_base_assistantmaterial_balancedeadline where BalanceDeadLineCode like ?";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);
			// 绑定宿主变量
			statement.setString(1, "%" + balancedeadlineCode + "%");
			// 执行查询，得到结果集
			resultSet = statement.executeQuery();
			// 处理结果集中的记录到采购价格model
			while (resultSet.next()) {
				PBaseAssistantmaterialBalancedeadline deadlineModel = new PBaseAssistantmaterialBalancedeadline();

				deadlineModel.setBalanceDeadLineId(resultSet.getInt(1));

				deadlineModel.setBalanceDeadLineCode(resultSet.getString(2));

				deadlineModel.setBalanceDeadLineName(resultSet.getString(3));

				deadlineList.add(deadlineModel);

			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return deadlineList;
	}

	/**
	 * 根据id模糊查询结算期限
	 */
	@Override
	public PBaseAssistantmaterialBalancedeadline getBalancedeadlineById(
			Integer balancedeadlineId) throws Throwable {
		// TODO Auto-generated method stub
		// 结算方式model
		PBaseAssistantmaterialBalancedeadline deadlineModel = new PBaseAssistantmaterialBalancedeadline();
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "select * from p_base_assistantmaterial_balancedeadline where BalanceDeadLineId =?";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);
			// 绑定宿主变量
			statement.setInt(1, balancedeadlineId);
			// 执行查询，得到结果集
			resultSet = statement.executeQuery();
			// 处理结果集中的记录到采购价格model
			if (resultSet.next()) {

				deadlineModel.setBalanceDeadLineId(resultSet.getInt(1));

				deadlineModel.setBalanceDeadLineCode(resultSet.getString(2));

				deadlineModel.setBalanceDeadLineName(resultSet.getString(3));
			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return deadlineModel;
	}

	/**
	 * 根据name模糊查询结算期限
	 */
	@Override
	public List<PBaseAssistantmaterialBalancedeadline> getBalancedeadlineByName(
			String balancedeadlineName) throws Throwable {
		// TODO Auto-generated method stub
		List<PBaseAssistantmaterialBalancedeadline> deadlineList = new ArrayList<PBaseAssistantmaterialBalancedeadline>();
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "select * from p_base_assistantmaterial_balancedeadline where BalanceDeadLineName like ?";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);
			// 绑定宿主变量
			statement.setString(1, "%" + balancedeadlineName + "%");
			// 执行查询，得到结果集
			resultSet = statement.executeQuery();
			// 处理结果集中的记录到采购价格model
			while (resultSet.next()) {
				PBaseAssistantmaterialBalancedeadline deadlineModel = new PBaseAssistantmaterialBalancedeadline();

				deadlineModel.setBalanceDeadLineId(resultSet.getInt(1));

				deadlineModel.setBalanceDeadLineCode(resultSet.getString(2));

				deadlineModel.setBalanceDeadLineName(resultSet.getString(3));

				deadlineList.add(deadlineModel);

			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return deadlineList;
	}

	/**
	 * 根据name和code模糊查询结算期限
	 */
	@Override
	public List<PBaseAssistantmaterialBalancedeadline> getBalancedeadlineByNameAndCode(
			String balancedeadlineName, String balancedeadlineCode)
			throws Throwable {
		// TODO Auto-generated method stub
		List<PBaseAssistantmaterialBalancedeadline> deadlineList = new ArrayList<PBaseAssistantmaterialBalancedeadline>();
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;
		// 结果集
		ResultSet resultSet = null;
		// 查询语句
		String sql = "select * from p_base_assistantmaterial_balancedeadline where BalanceDeadLineName like ? and BalanceDeadLineCode like ?";

		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql);
			// 绑定宿主变量
			statement.setString(1, "%" + balancedeadlineName + "%");
			statement.setString(2, "%" + balancedeadlineCode + "%");
			// 执行查询，得到结果集
			resultSet = statement.executeQuery();
			// 处理结果集中的记录到采购价格model
			while (resultSet.next()) {
				PBaseAssistantmaterialBalancedeadline deadlineModel = new PBaseAssistantmaterialBalancedeadline();

				deadlineModel.setBalanceDeadLineId(resultSet.getInt(1));

				deadlineModel.setBalanceDeadLineCode(resultSet.getString(2));

				deadlineModel.setBalanceDeadLineName(resultSet.getString(3));

				deadlineList.add(deadlineModel);

			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return deadlineList;
	}

	/**
	 * 更新结算期限
	 */
	@Override
	public void updateBalancedeadline(
			PBaseAssistantmaterialBalancedeadline balancedeadlineModel)
			throws Throwable {
		// TODO Auto-generated method stub
		// 数据库连接
		Connection con = null;
		// 预备语句
		PreparedStatement statement = null;

		ResultSet resultSet = null;
		// 修改sql语句
		StringBuilder sql = new StringBuilder();

		sql
				.append("update p_base_assistantmaterial_balancedeadline set BalanceDeadLineCode='");
		sql.append(balancedeadlineModel.getBalanceDeadLineCode() + "',");
		sql.append("BalanceDeadLineName='"
				+ balancedeadlineModel.getBalanceDeadLineName());
		sql.append("' where BalanceDeadLineId="
				+ balancedeadlineModel.getBalanceDeadLineId());
		try {

			con = this.dataSource.getConnection();

			statement = con.prepareStatement(sql.toString());
			statement.executeUpdate();

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, statement, resultSet);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
	}

}
